<template>
    <div class="components-container">
        <!-- 派车登记 -->
        <sweet-modal ref="dialog" class="dialog" :sweetModalMaxWidth="maxWidth" v-bind:blocking="true">
            <h3>{{title}}</h3>
            <div style="margin:10px 50px">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">业务编号</label>
                        <div class="layui-input-inline">
                            <el-input v-model="deaddata.bussNO" placeholder="请输入业务编号" @keyup.enter.native="getSendCarByNo" :readonly="txtBussNoReadonly"></el-input>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">逝者姓名</label>
                        <div class="layui-input-inline">
                            <el-input v-model="deaddata.deadName" readonly></el-input>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">逝者性别</label>
                        <div class="layui-input-inline">
                            <el-input v-model="deaddata.DEADSEXDESC" readonly></el-input>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">逝者年龄</label>
                        <div class="layui-input-inline">
                            <el-input v-model="deaddata.DEADAGE" readonly></el-input>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">联系人</label>
                        <div class="layui-input-inline">
                            <el-input v-model="deaddata.linkMan" readonly></el-input>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">联系电话</label>
                        <div class="layui-input-inline">
                            <el-input v-model="deaddata.linkTel" readonly></el-input>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">  
                        <label class="layui-form-label">接尸时间</label>
                        <div class="layui-input-inline">
                            <el-input v-model="deaddata.JSDATEDESC" readonly></el-input>  
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">接尸地点</label>
                        <div class="layui-input-inline">
                            <el-input v-model="deaddata.JSADDRESS" readonly></el-input> 
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">逝者关系</label>
                        <div class="layui-input-inline">
                            <el-input v-model="deaddata.RELATION" readonly></el-input>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-inline">
                            <el-input v-model="deaddata.deadRemark" type="textarea" class="longertext" readonly></el-input>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div style="overflow: auto; width: 100%;">
                        <table class="layui-table" style="border-spacing: 0;width:2000px;">
                            <colgroup>
                                <col width="65">
                                <col width="200">
                                <col width="270">
                                <col width="200">
                                <col width="230">
                                <col v-if="!showlic" width="120">
                                <col v-if="showlic" width="120">
                                <col width="200">
                                <col width="520">   <!-- 出车地点 -->
                                <col v-if="!showlic" width="65">
                                <col v-if="!showlic" width="65">
                                <col v-if="!showlic" width="65">
                                <col width="200" v-if="showlic">
                                <col width="200">
                                <col width="95">
                                <col width="100">
                            </colgroup>
                            <thead>
                                <tr>
                                    <th width="65">序号</th>
                                    <th>出车类型</th>
                                    <th>车牌号</th>
                                    <th>驾驶员</th>
                                    <th>接运员</th>
                                    <th v-if="!showlic">费用</th>
                                    <th v-if="showlic">里程</th>
                                    <th>出车时间</th>
                                    <th width="520">出车地点</th>
                                    <th v-if="!showlic">值班</th>
                                    <th v-if="!showlic">早班</th>
                                    <th style="text-align:center;" v-if="!showlic">晚班</th>
                                    <th v-if="showlic">超出时间</th>
                                    <th>备注</th>
                                    <th>操作</th>
                                    <th>打印操作</th>
                                </tr>
                            </thead>
                            <tbody ref='inputss'>
                                <tr class="allRow" v-for="(chl,index) in chlList">
                                    <td width="65">
                                        {{index + 1}}
                                    </td>
                                    <td>
                                        <el-select v-model="chl.CDHCODNAME" ref="select" placeholder="出车类型" @keydown.enter.native="enterEvent(index,0)">
                                            <el-option v-for="item in cdhTypes" :key="item.value" :label="item.label" :value="item.value">
                                            </el-option>
                                        </el-select>
                                    </td>
                                    <td>
                                        <!-- 车牌号 -->
                                        <el-cascader :options="carLics" v-model="chl.cardata" @change="cascaderchange(index,1,$event)"></el-cascader>
                                    </td>
                                    <td>
                                        <el-select v-model="chl.CDHDRIVER" placeholder="司机" @change="DriverChange($event,index)" @keydown.enter.native="enterEvent(index,2)">
                                            <el-option v-for="item in drivers" :key="item.value" :label="item.label" :value="item.value">
                                            </el-option>
                                        </el-select>
                                    </td>
                                    <td>
                                        <el-select v-model="chl.List_Carry" placeholder="接运员" multiple :multiple-limit=2>
                                            <el-option v-for="item in carriers" :key="item.value" :label="item.label" :value="item.value"></el-option>
                                        </el-select>
                                    </td>
                                    <td v-if="!showlic">
                                        <!-- 费用 -->
                                        <el-input @keyup.native="check(index)" v-model="chl.CDHDISPATCHINGCOST"></el-input>
                                    </td>
                                    <td v-if="showlic">
                                        <!-- 里程 -->
                                        <el-input  v-model="chl.CDHMILEAGE" oninput="value=value.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')"></el-input>
                                    </td>
                                    <td>
                                        <div class="layui-input-inline chooseTime">
                                            <el-date-picker style="width:115px;float:left;" clear-icon="null" type="date" v-date v-model="chl.cdhActualDateTime"></el-date-picker>
                                            <!-- @change="changetj($event)" -->
                                            <el-time-picker style="width:75px;float:left" clear-icon="null"  v-model="chl.cdhActualDateTime1" format="HH:mm"></el-time-picker>
                                        </div>
                                    </td>
                                    <td width="520">
                                        <el-select v-model="chl.CDHADDID" style="width:150px;float:left" placeholder="请选择"  @visible-change="selectAddr" filterable :filter-method="PinyinMatch">
                                            <el-option :key="item.CodeValue" :label="item.CodeName" :value="item.CodeValue" v-for="item in commonAddr"></el-option>
                                        </el-select>
                                        <el-input type="text" v-model="chl.CDHADDRESS" style="width:230px;float:left"></el-input>
                                        <!-- <el-select v-model="chl.CDHADDRESS" placeholder="请输入常用接尸地址" editable="true" filterable allow-create default-first-option :filter-method="PinyinMatch">
                                            <el-option :key="item.CodeValue" :label="item.CodeName" :value="item.CodeValue" v-for="item in commonAddr"></el-option>
                                        </el-select> -->
                                    </td>
                                    <td style="text-align:center;" v-if="!showlic">
                                        <!-- 值班 -->
                                        <el-checkbox v-model="chl.CDHISCHARGE" :border="true" size="medium"></el-checkbox>
                                    </td>
                                    <td v-if="!showlic"> 
                                        <!-- 早班 -->
                                        <el-checkbox v-model="chl.zaoban" :border="true" size="medium"></el-checkbox>
                                    </td>
                                    <td v-if="!showlic">
                                        <!-- 晚班 -->
                                        <el-checkbox v-model="chl.wanban" :border="true" size="medium"></el-checkbox>
                                    </td>
                                    <td v-if="showlic">
                                        <!-- 超出时间 -->
                                        <el-input v-model="chl.CDHOUTTIM" oninput="value=value.replace(/^\D*(\d*(?:\.\d{0,1})?).*$/g, '$1')" >
                                        </el-input>
                                    </td>
                                    <td>
                                        <!-- 备注 -->
                                        <el-input @keyup.native="check(index)" v-model="chl.CDHREMARKS">
                                        </el-input>
                                    </td>
                                    <td>
                                        <!-- 操作 -->
                                        <button class="layui-btn layui-btn-small layui-btn-normal" @click="remove(index,chl)">
                                            <i class="layui-icon">&#xe640;</i>
                                        </button>
                                    </td>
                                    <td>
                                    <el-button v-if="chl.CDHID" size="mini" type="primary" @click="clickpdf(chl.CDHID)" >打印</el-button>
                                    </td>
                                </tr>
                            </tbody>
                            <tfoot>
                                <tr>
                                    <td colspan="1">
                                        <span>合计</span>
                                    </td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td v-if="!showlic"></td>
                                    <td v-if="showlic"></td>
                                    <td></td>
                                    <td width="520"></td>
                                    <td v-if="!showlic"></td>
                                    <td v-if="!showlic"></td>
                                    <td v-if="!showlic"></td>
                                    <td v-if="showlic">{{ discountTotal }}</td>
                                    <td>{{ discountTotal }}</td>
                                    <td>
                                        <button class="layui-btn layui-btn-small layui-btn-normal" @click="insert()">
                                            <i class="layui-icon">&#xe654;</i>
                                        </button>
                                    </td>
                                    <td></td>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
            <div class="layui-form-item btnCenter">
                <el-button @click="cancel">取消</el-button>
                <el-button type="primary" @click="submit">确定</el-button>
            </div>
        </sweet-modal>
        <!-- 详情 -->
        <sweet-modal ref="info" class="dialog" :sweetModalMaxWidth="maxWidth" v-bind:blocking="true">
            <h3>{{title}}</h3>
            <div style="margin:10px 50px">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">业务编号</label>
                        <div class="layui-input-inline">
                            <el-input v-model="deaddata.bussNO" readonly></el-input>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">逝者姓名</label>
                        <div class="layui-input-inline">
                            <el-input v-model="deaddata.deadName" readonly></el-input>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">逝者性别</label>
                        <div class="layui-input-inline">
                            <el-input v-model="deaddata.DEADSEXDESC" readonly></el-input>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">逝者年龄</label>
                        <div class="layui-input-inline">
                            <el-input v-model="deaddata.DEADAGE" readonly></el-input>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">联系人</label>
                        <div class="layui-input-inline">
                            <el-input v-model="deaddata.linkMan" readonly></el-input>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">联系电话</label>
                        <div class="layui-input-inline">
                            <el-input v-model="deaddata.linkTel" readonly></el-input>
                        </div>
                    </div>
                </div>
                 <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">接尸时间</label>
                        <div class="layui-input-inline">
                            <el-input v-model="deaddata.JSDATEDESC" readonly></el-input>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">接尸地点</label>
                        <div class="layui-input-inline">
                            <el-input v-model="deaddata.JSADDRESS" readonly></el-input>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">逝者关系</label>
                        <div class="layui-input-inline">
                            <el-input v-model="deaddata.RELATION" readonly></el-input>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-inline">
                            <el-input v-model="deaddata.deadRemark" class="longertext" readonly></el-input>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div style="overflow: auto; width: 100%;">
                        <table class="layui-table" style="border-spacing: 0;width:2000px;">
                            <colgroup>
                                <col width="65">
                                <col width="200">
                                <col width="270">
                                <col width="200">
                                <col width="230">
                                <col v-if="!showlic" width="120">
                                <col v-if="showlic" width="120">
                                <col width="200">
                                <col width="520">
                                <col v-if="!showlic" width="65">
                                <col v-if="!showlic" width="65">
                                <col v-if="!showlic" width="65">
                                <col width="200" v-if="showlic">
                                <col width="200">
                                <col width="95">
                                <col width="100">
                            </colgroup>
                            <thead>
                                <tr>
                                    <th>序号</th>
                                    <th>出车类型</th>
                                    <th>车牌号</th>
                                    <th>驾驶员</th>
                                    <th>接运员</th>
                                    <th v-if="!showlic">费用</th>
                                    <th v-if="showlic">里程</th>
                                    <th>出车时间</th>
                                    <th>出车地点</th>
                                    <th v-if="!showlic">值班</th>
                                    <th v-if="!showlic">早班</th>
                                    <th style="text-align:center;" v-if="!showlic">晚班</th>
                                    <th v-if="showlic">超出时间</th>
                                    <th>备注</th>
                                    <th>操作</th>
                                    <th>打印操作</th>
                                </tr>
                            </thead>
                            <tbody ref='inputs'>
                                <tr class="allRow" v-for="(chl,index) in chlList">
                                    <td>
                                        {{index + 1}}
                                    </td>
                                    <td>
                                        <el-select v-model="chl.CDHCODNAME" placeholder="出车类型" disabled>
                                            <el-option v-for="item in cdhTypes" :key="item.value" :label="item.label" :value="item.value">
                                            </el-option>
                                        </el-select>
                                    </td>
                                    <td>
                                        <!-- 车牌号 -->
                                        <el-cascader :options="carLics" v-model="chl.cardata" disabled></el-cascader>
                                    </td>
                                    <td>
                                        <el-select v-model="chl.CDHDRIVER" placeholder="司机" disabled>
                                            <el-option v-for="item in drivers" :key="item.value" :label="item.label" :value="item.value">
                                            </el-option>
                                        </el-select>
                                    </td>
                                    <td>
                                        <el-select v-model="chl.List_Carry" placeholder="接运员" multiple :multiple-limit=2 disabled>
                                            <el-option v-for="item in carriers" :key="item.value" :label="item.label" :value="item.value"></el-option>
                                        </el-select>
                                    </td>
                                    <td v-if="!showlic">
                                        <!-- 费用 -->
                                        <el-input @keyup.native="check(index)" v-model="chl.CDHDISPATCHINGCOST" disabled></el-input>
                                    </td>
                                    <td v-if="showlic">
                                        <!-- 里程 -->
                                        <el-input  v-model="chl.CDHMILEAGE" oninput="value=value.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')"></el-input>
                                    </td>
                                    <td>
                                        <div class="layui-input-inline chooseTime">
                                            <el-date-picker style="width:115px;float:left;" clear-icon="null" type="date" v-date v-model="chl.cdhActualDateTime" disabled></el-date-picker>
                                            <!-- @change="changetj($event)" -->
                                            <el-time-picker style="width:75px;float:left" clear-icon="null"  v-model="chl.cdhActualDateTime1" format="HH:mm" disabled></el-time-picker>
                                        </div>
                                    </td>
                                    <td> 
                                        <el-select v-model="chl.CDHADDID" style="width:150px;float:left" placeholder="请选择" disabled  @visible-change="selectAddr" filterable :filter-method="PinyinMatch">
                                            <el-option :key="item.CodeValue" :label="item.CodeName" :value="item.CodeValue" v-for="item in commonAddr"></el-option>
                                        </el-select>
                                        <el-input disabled type="text" v-model="chl.CDHADDRESS" style="width:230px;float:left"></el-input>
                                    </td>
                                    <td style="text-align:center;" v-if="!showlic">
                                        <!-- 值班 -->
                                        <el-checkbox v-model="chl.CDHISCHARGE" :border="true" size="medium" disabled></el-checkbox>
                                    </td>
                                    <td v-if="!showlic">
                                        <!-- 早班 -->
                                        <el-checkbox v-model="chl.zaoban" :border="true" size="medium" disabled></el-checkbox>
                                    </td>
                                    <td v-if="!showlic">
                                        <!-- 晚班 -->
                                        <el-checkbox v-model="chl.wanban" :border="true" size="medium" disabled></el-checkbox>
                                    </td>
                                    <td v-if="showlic">
                                        <!-- 超出时间 -->
                                        <el-input v-model="chl.CDHOUTTIM" oninput="value=value.replace(/^\D*(\d*(?:\.\d{0,1})?).*$/g, '$1')" >
                                        </el-input>
                                    </td>
                                    <td>
                                        <!-- 备注 -->
                                        <el-input v-model="chl.CDHREMARKS" disabled>
                                        </el-input>
                                    </td>
                                    <td>
                                        <!-- 操作 -->
                                        <button class="layui-btn layui-btn-small layui-btn-normal" @click="remove(index)" disabled>
                                            <i class="layui-icon">&#xe640;</i>
                                        </button>
                                    </td>
                                     <td>
                                    <el-button v-if="chl.CDHID" size="mini" type="primary" @click="clickpdf(chl.CDHID)" >打印</el-button>
                                    </td>
                                </tr>
                            </tbody>
                            <tfoot>
                             <tr>
                                    <td colspan="1">
                                        <span>合计</span>
                                    </td>
                                    <td>
                                    </td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td v-if="!showlic"></td>
                                    <td v-if="showlic"></td>
                                    <td></td>
                                    <td></td>
                                    <td v-if="!showlic"></td>
                                    <td v-if="!showlic"></td>
                                    <td v-if="!showlic"></td>
                                    <td v-if="showlic">{{ discountTotal }}</td>
                                    <td>{{ discountTotal }}</td>
                                    <td>
                                        <button class="layui-btn layui-btn-small layui-btn-normal" @click="insert()" disabled>
                                            <i class="layui-icon">&#xe654;</i>
                                        </button>
                                    </td>
                                    <td></td>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
            <div class="layui-form-item btnCenter">
                <el-button @click="infoClose">确定</el-button>
            </div>
        </sweet-modal>
        <!-- 数据合并 -->
        <sweet-modal ref="dialogMerge" class="dialog" :sweetModalMaxWidth="800" v-bind:blocking="true">
        <h3>{{title}}</h3>
        <fieldset class="layui-elem-field layui-field-title">
            <legend>请点击要合并的数据并提交</legend>
        </fieldset>
        <el-table
            ref="singleTable"
            :data="tableData"
            height="350"
            border
            highlight-current-row
            @current-change="handleCurrentChange"
            style="width: 100%"
            >
            <el-table-column
            type="index"
            width="50">
            </el-table-column>
            <el-table-column  v-for="(item,index) in headList" :key="index"
            :property="item.ColumnKey"
            :label="item.ColumnName"
            width="120">
            </el-table-column>
        </el-table>
        <div class="layui-form-item btnCenter">
            <el-button @click="closeMergeDialog">取消</el-button>
            <el-button type="primary" icon="el-icon-check" :loading="this.$store.state.isloading"  @click="dataMerge">提交</el-button>
        </div>
        </sweet-modal>
         <printmodal ref="printPdf"></printmodal>
    </div>
</template>

<style rel="stylesheet/scss" lang="scss" scoped>
.el-checkbox.is-bordered.el-checkbox--medium {
    padding: 7px 10px 7px 10px;
}
</style>
<script>
import sweetModal from "@/components/sweetModal";
import apiBase from "@/api/apiBase.js";
import { accMul, Subtr, accAdd } from "@/utils/operations.js";
import { formatDate, timeStringToDate } from "@/utils/date.js";
import { manage } from "@/mixins/managePage.js";
import { PRINT } from "@/components/Pdf/PDF.js";
import printmodal from "@/components/Pdf/Print";
const ADD = "新增";
const CARSEND = "派车";
const MODIFY = "修改";
const DELETE = "删除";
const INFO = "详情";
const PRINTCARSEND = "打印派车单";
const onecar = "普通车";
const twocar = "高档车";
const threecar = "自送";
export default {
    name: "carSendMain",
    mixins: [manage],
    data() {
        return {
            mergeid:null,
            headList:[], 
            tableData: [],
            currentRow: null,tableData:[],
            prpid:null,
            showlic:null,  //派车登记里程、超出时间新加 去除早晚班 
            maxWidth: 1200,
            pageWidth: 1200,
            maxHeight: 520,
            printUrl: null,
            commonAddr: [],
            copycommonAddr: [],
            pickupcardatas: [], //车牌号
            allCharge: [],

            //收费项目
            chlList: [],
            chaList: [
                {
                    CHANAME: "1",
                    CHAID: "1"
                }
            ],
            chl: {
                CDHCODNAME: null, //出车类型
                CDHDRIVER: null,//驾驶员
                CDHDRIVERINFO: null,//详情中的驾驶员
                CDHADDID: null,//接尸地址id
                CDHADDRESS: null,//接尸地址
                CDHISCHARGE: false,//是否值班
                zaoban: false,//早班
                wanban: false,//晚班
                CDHREMARKS: null,//备注
                CDHOUTTIM: null,//超出时间 余姚个性化
                cardata: [],
                cardatainfo: [],
                cdhActualDateTime: null,//出车时间
                cdhActualDateTime1: null,//出车时间  -时间
                CDHDISPATCHINGCOST: 0, //费用
                List_Carry: [], // 接运员
                CDHMILEAGE:null, //里程
            },

            cdhdata: {
                
            },
            cdhextend: {
                
            },
            
            // cdhGobackTime: null,
            cdhEndDateTime: null,
            deaddata: {
                bussID: null,
                bussNO: null,
                deadName: null,
                DEADSEXDESC: null,
                DEADAGE: null,
                JSDATEDESC: null,
                JSADDRESS: null,
                RELATION: null,
                linkMan: null,
                linkTel: null,
                deadRemark: null,
                pcrAddress: null,
                pcrDate: null,
                pcrCarType: null,
                cdhList: []
            },
            type: null,
            carLics: [],
            carriers: [],
            cdhstatus: [],
            drivers: [],
            censusTypes: [],
            bussStatus: [],
            cdhTypes: [],
            title: null,
            cdhtitle: null,
            cdhsLists: null, //保存逝者信息和派车信息
        };
    },
    methods: {
        // 义乌派车页面 出车地址 取到下拉值再可输入
          selectAddr() {
              this.chlList.forEach(item=>{
              if(item.CDHADDID){  item.CDHADDRESS = item.CDHADDID + "";}
              })
           
        },
        //下拉框设置拼音简码跳出
        PinyinMatch(val){
            const PinyinMatch = require('pinyin-match');
           if (val) {
                var result = []
                this.copycommonAddr.forEach(i => { 
                    var m = PinyinMatch.match(i.CodeName, val)
                    if (m) {
                        result.push(i);
                    }
                })
                this.commonAddr = result;
            }else{
                this.commonAddr = this.copycommonAddr;   
            }
        },
        //回车事件
        enterEvent(rowNum, colNum) {
            //获取所有行
            let rows = this.$refs.inputss.getElementsByClassName("allRow");
            //获取当前行
            let currow = rows[rowNum];
            // 获取当前行所有列
            let inputs = currow.getElementsByTagName("input");
            //强制隐藏下拉框
            if (colNum === 0) {
                let chl = this.chlList[rowNum];
                if (chl.CDHCODNAME.length === 0) {
                    this.$message({
                        type: "error",
                        message: `第${parseInt(rowNum) + 1}行收费项目不能为空!`
                    });
                    return;
                }
                let select = this.$refs.select;
                select[rowNum].visible = false;
            }
            //最后一列
            if (colNum === inputs.length - 1) {
                //最后一行
                if (rowNum === this.chlList.length - 1) {
                    //插入空行
                    if (!this.insert()) {
                        return;
                    }
                }
                //换行
                this.$nextTick(function() {
                    this.enterEvent(rowNum + 1, -1);
                });
            } else {
                //获取下一个焦点
                this.$nextTick(function() {
                    inputs[colNum + 1].select();
                });
            }
        },
        cascaderchange(rowNum, colNum,val){
             //获取所有行
            let rows = this.$refs.inputss.getElementsByClassName("allRow");
            //获取当前行
            let currow = rows[rowNum];
            // 获取当前行所有列
            let inputs = currow.getElementsByTagName("input");
            this.$nextTick(function() {
                inputs[colNum + 1].select();
            });
            this.carLics.forEach(item => {
                if(val[0] == item.value){
                    item.children.forEach(list => {
                        if(val[1] == list.value){
                            let chl = this.chlList[rowNum];
                            chl.CDHDRIVER = list.CARDRIVER;
                            this.DriverChange(chl.CDHDRIVER,rowNum);
                        }
                    });
                }
            });
        },
        //自定义筛选
        chaFilter(val) {
            this.chaList = JSON.parse(JSON.stringify(this.allCharge));
            if (val) {
                if (val.length > 0)
                    this.chaList = this.allCharge.filter(item => {
                        return (
                            item.CHANAME.indexOf(val) > -1 ||
                            item.CHADM.toLowerCase().indexOf(
                                val.toLowerCase()
                            ) > -1
                        );
                    });
            }
        },
        //新增行
        insert() {
            if(this.chlList.length > 0){
                for (let index in this.chlList) {
                    let chl = this.chlList[index];
                    if (chl.CDHCODNAME === null) {
                        this.$message({
                            type: "error",
                            message: `第${parseInt(index) + 1}行收费项目不能为空!`
                        });
                        return false;
                    }
                }
            };
            this.chl.CDHADDID = this.cdhsLists.JSADDID;
            this.chl.CDHADDRESS = this.cdhsLists.JSADDRESS;
            this.chlList.push(JSON.parse(JSON.stringify(this.chl)));
            this.chlList[this.chlList.length-1].cdhActualDateTime=timeStringToDate(
                //修改中的到馆时间
                this.cdhsLists.JSTIMEDESC,
                this.cdhsLists.JSDATEDESC
            );
            this.chlList[this.chlList.length-1].cdhActualDateTime1=timeStringToDate(
                //修改中的到馆时间
                this.cdhsLists.JSTIMEDESC,
                this.cdhsLists.JSDATEDESC
            );
            return true;
        },
        remove(index,chl) {
            if(chl.CDHID){
                this.$confirm("确认是否删除该条派车数据?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                .then(callback => {
                    let data = {
                        CDHID: chl.CDHID
                    };
                    apiBase.DoService(
                        data,
                        apiBase.getApiAdress().Sendcar.Delete,
                        res => {
                            this.$message.success("删除成功");
                            this.chlList.splice(index, 1);
                        }
                    );
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消"
                    });
                });
            }else{
                this.chlList.splice(index, 1);
            }
            
        },
        check(index) {
            let chl = this.chlList[index];
            chl.CDHDISPATCHINGCOST = this.checkNumber(chl.CDHDISPATCHINGCOST.toString());
            chl.CDHMILEAGE = this.checkNumber(chl.CDHMILEAGE.toString());
            chl.CDHOUTTIM = this.checkNumber(chl.CDHOUTTIM.toString());
        },
         //只能输入正整数
        checkNumber(value) {
            value = value.replace(/\D/g, "").replace(/^0+(?=\d)/, "");
            if (value !== "") {
                if (value.substr(0, 1) === "0" && value.length === 2) {
                    value = value.substr(1, value.length);
                }
            } else {
                value = 0;
            }
            return value;
        },
        clickpdf(id){
            this.$refs.printPdf.open('打印按钮派车单',null,id); 
        },
        open(order, id,currentRow) {
            this.order = order;
            this.cdhorder = order;
            this.prpid=currentRow.BUSSID
            this.initSelect();//初始化下拉框
            switch (this.order) {
                case "派车修改":  //宁波个性化出车汇总修改
                    this.title = "派车登记修改";
                    this.deaddata.bussNO = id;
                    this.chlList = [];
                    this.getSendCarByNo();
                    // this.insert();
                    this.$refs.dialog.open();
                    break;
                case "义乌派车":
                    this.title = "派车登记";
                    this.deaddata.bussNO = id;
                    this.chlList = [];
                    this.getSendCarByNo();
                    // this.insert();
                    if(currentRow.PRPSTATUS=="A"&&currentRow.REGFORMID=="A"){
                      this.getColumns();
                      this.getDatamerge(currentRow.BUSSID)
                    }else{
                    this.$refs.dialog.open();
                    }
                    break;
                case "义乌详情":
                    this.title = "派车详情";
                    this.deaddata.bussNO = id;
                    this.getSendCarByNo();
                    this.$refs.info.open();
                    break;
                default:
            }
        },
        operateCdh(order, cdhid) {
            this.cdhorder = order;
            if (cdhid) this.cdhdata.CDHID = cdhid;
            switch (this.cdhorder) {
                case ADD:
                    this.cdhtitle = "派车新增";
                    this.CdhdataRepeat();
                    this.initSelect();
                    this.$refs.dialog1.open();
                    break;
                // case MODIFY:
                //     this.cdhtitle = "派车修改";
                //     this.initSelect();
                //     this.dataDetail();
                //     this.$refs.dialog1.open();
                //     break;
                case PRINTCARSEND:
                    this.cdhtitle = "派车单打印";
                    //this.cdhorder, null, this.cdhdata.CDHID
                    this.getPRINTSEND();
                    this.$refs.dialogPrint.open();
                    break;
                default:
            }
        },
        //获取表头
        getColumns() {
        let data = {};
        apiBase.DoService(
            data,
            apiBase.getApiAdress().Column.GetReserveMergeColumns,
            res => {
            this.headList = res.data.content;
            }
        );
        },
        getDatamerge(id){
            let dataid={
                PRPID:id
            }
            apiBase.DoService(
            dataid,
            apiBase.getApiAdress().Reserve.QueryReserveRepeat,
            res => {
                this.tableData = res.data.content;
                if(res.data.content.length!=0){
                    this.$refs.dialogMerge.open();
                }else{
                    this.$refs.dialog.open();
                }
            });
        },
        handleCurrentChange(val) {
        if(val&&val.PRPID){
        this.mergeid=val.PRPID
        this.currentRow = val;
        }
        },
        closeMergeDialog(){
            this.$refs.dialogMerge.close(); 
            this.$refs.dialog.open();
        },
        //数据合并提交
        dataMerge(){
            let Data={
                PRPID:this.prpid,
                MERGEID:this.mergeid,
            }
                apiBase.DoService(
                Data,
                apiBase.getApiAdress().Reserve.ReserveMerge,
                res => {
                    this.$refs.dialogMerge.close(); 
                    this.$refs.dialog.open();
                    this.$message({
                        type: "success",
                        message: "合并成功!"
                    });
                    this.refreshData();
                }
            );
        },
        //提交
        submit() {
            switch (this.cdhorder) {
                case ADD:
                case MODIFY:
                case '义乌派车':
                    this.EditSendCar();
                    break;
                case DELETE:
                    break;
                case "派车修改":      //宁波个性化出车汇总修改
                this.EditSendCar();
                break;    
                default:
            }
        },
        //初始化下拉框
        initSelect() {
            this.getSendCdhTypes();
            this.getSendCdhStatus();
            this.getCar();
            this.getDrivers();
            this.getBussStatus();
            this.getCarriers();
            this.getCensusTypes();
            this.getCommonAddr();
        },
        //根据预约号和业务编号获取派车信息
        getSendCarByNo() {
            let data = {
                BussNo: this.deaddata.bussNO
            };
            apiBase.DoService(
                data,
                apiBase.getApiAdress().Sendcar.GetSendCarByNo,     
                res => {
                    let cdhsList = res.data.content;
                    this.cdhsLists = JSON.parse(JSON.stringify(cdhsList))
                    this.deaddata.bussID = cdhsList.BUSSID;
                    this.deaddata.deadName = cdhsList.DEADNAME;
                    this.deaddata.DEADSEXDESC = cdhsList.DEADSEXDESC;
                    this.deaddata.DEADAGE = cdhsList.DEADAGE;
                    this.deaddata.JSDATEDESC = cdhsList.JSDATEDESC; //接尸时间
                    this.deaddata.JSADDID = cdhsList.JSADDID;  //接尸地点id
                    this.deaddata.JSADDRESS = cdhsList.JSADDRESS;  //接尸地点
                    this.deaddata.RELATION = cdhsList.RELATION;
                    this.deaddata.linkMan = cdhsList.LINKMAN;
                    this.deaddata.linkTel = cdhsList.LINKPHONE;
                    this.deaddata.deadRemark = cdhsList.REMARK;
                    if(this.title === "派车登记"){
                        setTimeout(() => {
                            this.insert();
                        }, 100);
                    }   

                    // this.chlList = cdhsList.Cdhs; 
                    let chlLists = cdhsList.Cdhs; 
                    //获取车辆
                    for(var i = 0;i < chlLists.length;i++){
                        chlLists[i].cardata = [chlLists[i].CARCODNAME,chlLists[i].CDHCARID];//车牌号
                        chlLists[i].cdhActualDateTime = timeStringToDate(chlLists[i].CDHACTUALTIME,chlLists[i].CDHACTUALDATE);//派车日期
                        chlLists[i].cdhActualDateTime1 = timeStringToDate(chlLists[i].CDHACTUALTIME,chlLists[i].CDHACTUALDATE);//派车日期  时间
                        if(chlLists[i].cdhActualDateTime1 == null){
                            chlLists[i].cdhActualDateTime = new Date();
                            chlLists[i].cdhActualDateTime1 = new Date();
                        }
                    } 
                    this.chlList = chlLists;
                    this.chlList.forEach((item) => {
                        if(item.CDHDRIVER && item.List_Carry.length===0){
                            item.List_Carry[0]=item.CDHDRIVER
                        }
                        item.List_Carry.forEach((Carryitem,Carryindex)=>{
                            this.drivers.forEach((val)=>{
                                if(val.value === Carryitem){
                                    item.List_Carry[Carryindex] = val.label
                                }
                            })
                        })
                    })
                }
            );
        },
        //添加修改派车记录
        EditSendCar() {
            let data = JSON.parse(JSON.stringify(this.chlList));
            data.forEach((item,index) => {
                item.BUSSNO = this.deaddata.bussNO;
                item.CDHACTUALDATE = formatDate(item.cdhActualDateTime, "yyyyMMdd");
                item.CDHACTUALTIME = formatDate(item.cdhActualDateTime1, "hhmmss"); 
                item.BUSSID = this.deaddata.bussID;
                item.CDHCARID = item.cardata[
                    item.cardata.length - 1
                ];
                item.CDHISPRINT = null,
                item.CensName = null;
                item.CDHCENSUS = null;
                item.List_Census = [];
                if(item.CDHISCHARGE === true){
                    item.List_Census.push('值班');
                    item.CDHISCHARGE = '1';
                }else{
                    item.CDHISCHARGE = '0';
                };
                if(item.zaoban === true){
                    item.List_Census.push('早班');
                };
                if(item.wanban === true){
                    item.List_Census.push('晚班');
                };
                if(item.CDHCODNAME == null){
                    data.splice(index, 1);
                }
                item.List_Carry.forEach((Carryitem,Carryindex) => {
                    this.drivers.forEach((val) => {
                        if(val.label === Carryitem){
                            item.List_Carry[Carryindex] = val.value
                        }
                    })
                })
                
            }) 
            if (this.cdhorder == ADD || this.cdhorder == "义乌派车"||this.cdhorder == "派车修改") {
                apiBase.DoService(
                    data,
                    apiBase.getApiAdress().Sendcar.CreateList,
                    res => {
                        this.getSendCarByNo();
                        this.$message.success("派车成功");
                        this.$refs.dialog.close();
                        this.refreshData();  // 余姚出车汇总 派车修改刷新页面
                    },
                    res => {
                        this.$message.error(res.data.msg);
                    }
                );
            } else if (this.cdhorder == MODIFY) {
                apiBase.DoService(
                    data,
                    apiBase.getApiAdress().Sendcar.Modify,
                    res => {
                        if (this.$route.path === "/carSend") {
                            this.getSendCarByNo();
                        }
                        this.$message.success("修改成功");
                        this.$refs.dialog1.close();
                    },
                    res => {
                        this.$message.error(res.data.msg);
                    }
                );
            }
        },
        DriverChange(val,index){   
            if(this.chlList[index].CDHCODNAME == '接尸' || this.chlList[index].CDHCODNAME == '送棺' || this.chlList[index].CDHCODNAME == '跑空' || this.chlList[index].CDHCODNAME == '送遗容床'){
                let data = val
                this.chlList[index].List_Carry.forEach((item,itemindex)=>{
                    this.drivers.forEach((driversitem) => {
                        if(driversitem.label === item){
                            this.chlList[index].List_Carry.splice(itemindex,1);
                        }
                    })
                })
                this.drivers.forEach((item) => {
                    if(item.value === data){
                        data = item.label
                    }
                })
                if(this.chlList[index].List_Carry.length < 2){
                    if(data == '') return;
                    this.chlList[index].List_Carry.push(data);
                }
            }else{
                this.chlList[index].List_Carry = [];
                return;
            }
        },
     
        //详情 --派车
        // dataDetail() {
        //     var data = {
        //         CDHID: this.cdhdata.CDHID
        //     };
        //     apiBase.DoService(
        //         data,
        //         apiBase.getApiAdress().Sendcar.Detail,
        //         res => {
        //             this.cdhdata = res.data.content;
        //             this.cdhActualDateTime = timeStringToDate(
        //                 this.cdhdata.CDHACTUALTIME,
        //                 this.cdhdata.CDHACTUALDATE
        //             );
        //             this.cdhEndDateTime = timeStringToDate(
        //                 this.cdhdata.CDHENDTIME,
        //                 this.cdhdata.CDHENDDATE
        //             );

        //             if (!this.cdhdata.List_Census) this.cdhdata.List_Census;

        //             this.cdhdata.CDHACTUALTIME = timeStringToDate(
        //                 this.cdhdata.CDHACTUALTIME
        //             );
        //             this.cdhdata.CDHENDTIME = timeStringToDate(
        //                 this.cdhdata.CDHENDTIME
        //             );
        //             this.cdhextend.cardata = [
        //                 this.cdhdata.CARCODNAME,
        //                 this.cdhdata.CDHCARID
        //             ];
        //             let findarry = this.carLics.filter(item => {
        //                 return item.value === this.cdhdata.CARCODNAME;
        //             });
        //             if (findarry.length > 0) {
        //                 let seachCarType = findarry[0];
        //                 if (
        //                     !seachCarType.children.some(item => {
        //                         return item.value === this.cdhdata.CDHCARID;
        //                     })
        //                 )
        //                     seachCarType.children.push({
        //                         label: this.cdhdata.CARLIC,
        //                         value: this.cdhdata.CDHCARID
        //                     });
        //             }
        //             if (
        //                 !this.drivers.some(item => {
        //                     return item.value === this.cdhdata.CDHDRIVER;
        //                 })
        //             )
        //                 this.drivers.push({
        //                     label: this.cdhdata.STANAME,
        //                     value: this.cdhdata.CDHDRIVER
        //                 });
        //         }
        //     );
        // },
       
      
        //派车数据重置
        CdhdataRepeat() {
            Object.assign(this.$data.cdhdata, this.$options.data().cdhdata);
            Object.assign(this.$data.cdhextend, this.$options.data().cdhextend);
            if (this.cdhorder === ADD) {
                this.cdhdata.CDHADDRESS = this.deaddata.pcrAddress;
                this.cdhdata.CDHACTUALDATE = this.deaddata.pcrDate;
                this.cdhextend.cardata = [this.deaddata.pcrCarType, ""];
            }
            this.cdhActualDateTime = new Date();
            this.cdhActualDateTime1 = new Date();
            this.cdhEndDateTime = null;
        },
        //获取派车类别
        getSendCdhTypes() {
            let data = {};
            apiBase.DoService(
                data,
                apiBase.getApiAdress().Code.GetSendCdhType,
                res => {
                    let cdhTypeData = res.data.content,
                        legendsCdhData = cdhTypeData.map(item => ({
                            label: item.CodeName,
                            value: item.CodeValue
                        }));
                    this.cdhTypes = legendsCdhData;
                }
            );
        },
        //获取常用地址
        getCommonAddr() {
            let data = {};
            apiBase.DoService(
                data,
                apiBase.getApiAdress().Code.GetCommonAddress,
                res => {
                    this.commonAddr = res.data.content;
                    this.copycommonAddr = res.data.content;
                }
            );
        },
        //获取派车状态
        getSendCdhStatus() {
            let data = {};
            apiBase.DoService(
                data,
                apiBase.getApiAdress().Sendcar.GetCdhStatus,
                res => {
                    this.cdhstatus = res.data.content;
                }
            );
        },
       
        //获取车辆信息
        getCar() {
            let data = {};
            apiBase.DoService(
                data,
                apiBase.getApiAdress().Car.QueryCarByType,
                res => {
                    let cardata = res.data.content;
                    let legendsCarData = cardata.map(item => ({
                        label: item.CodeName,
                        value: item.CodeValue,
                        children: item.list_Cars.map(caritem => ({
                            label: caritem.CARLIC,
                            value: caritem.CARID,
                            CARDRIVER: caritem.CARDRIVER
                        }))
                    }));
                    this.carLics = legendsCarData;
                }
            );
        },
        //获取司机
        getDrivers() {
            let data = {};
            apiBase.DoService(
                data,
                apiBase.getApiAdress().Staff.QueryDriver,
                res => {
                    let driverData = res.data.content,
                        legendsDrivers = driverData.map(item => ({
                            label: item.STANAME,
                            value: item.STAID
                        }));
                    this.drivers = legendsDrivers;
                }
            );
        },

        //获取业务状态
        getBussStatus() {
            let data = {};
            apiBase.DoService(
                data,
                apiBase.getApiAdress().Sendcar.GetBusstype,
                res => {
                    this.bussStatus = res.data.content;
                }
            );
        },
        //获取接运员
        getCarriers() {
            let data = {};
            apiBase.DoService(
                data,
                apiBase.getApiAdress().Staff.QueryCarry,
                res => {
                    let carryData = res.data.content,
                        legendsCarriers = carryData.map(item => ({
                            label: item.STANAME,
                            value: item.STAID
                        }));
                    this.carriers = legendsCarriers;
                }
            );
        },
        //获取派车统计编码
        getCensusTypes() {
            let data = {};
            apiBase.DoService(
                data,
                apiBase.getApiAdress().Code.GetCensustype,
                res => {
                    let censusTypeData = res.data.content,
                        legendsCensusData = censusTypeData.map(item => ({
                            label: item.CodeName,
                            value: item.CodeValue
                        }));
                    this.censusTypes = legendsCensusData;
                }
            );
        },
        //获取接尸通知单URL
        getPRINTSEND() {
            let data = {
                PRPID: null,
                CDHID: this.cdhdata.CDHID
            };
            apiBase.DoService(
                data,
                apiBase.getApiAdress().Pdf.PrintCdh,
                res => {
                    this.printUrl = res.data.content.PDFNAME;
                    this.$refs.dialogPrint.open();
                }
            );
        },
        //关闭派车信息弹窗
        cancel1() {
            this.$refs.dialog1.close();
        },
        //余姚个性化 新增里程 超出时间 
        getyuyao() {
            let data = {};
            apiBase.DoService(
                data,
                apiBase.getApiAdress().Param.GetIsShowMileage,
                res => {
                    this.showlic = res.data.content;
                }
            );
        },
        refreshData() {   //刷新页面
            this.$emit("refreshData");
        }
    },
    components: {
        sweetModal,
        printmodal
    },
    computed: {
        txtBussNoReadonly() {
            return this.order === MODIFY;
        },
        discountTotal() {
            let total = 0;
            for (let chl of this.chlList) {
                if(chl.CDHDISPATCHINGCOST){
                    total = accAdd(total, chl.CDHDISPATCHINGCOST);
                }else{
                    // total = 0;
                }
            }
            return total;
        }
    },
    activated() {
        this.getyuyao()
    },
    mounted() {
        this.getDrivers();
        this.getCar();
    },
    created() {
      this.getyuyao()
    },
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/common.scss";
.el-form-item {
    margin-bottom: 0px;
}
.div-card {
    max-height: 300px;
    overflow: auto;
}
.div-card label {
    min-width: 100px;
    font-weight: 400;
    padding-left: 20px;
}
.btnCenter {
    text-align: center;
}
.longtext {
    width: 480px;
}
.longertext {
    width: 770px;
}
</style>

